package com.wlstock.funi.request;

import com.android.volley.NetworkResponse;
import com.android.volley.Request.Method;
import com.android.volley.VolleyError;
import com.wlstock.funi.constant.URLConstant;
import com.wlstock.funi.utils.HttpUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.zip.GZIPInputStream;

public class WenGuRequest implements NetWorkListener, ParseNetResponseListener {
	
	public NetWorkListener netWorkListener;
	
	public void request(String key,String reqTag) {
		CustomStringRequest curRequest = new CustomStringRequest();
		curRequest.networkResponseListener = netWorkListener!=null?netWorkListener:this;
		curRequest.parseListener = this;
		HashMap<String, String> header = requestHeader();
		HashMap<String, String> params = requestParams(key);
		curRequest.request(Method.GET, URLConstant.WENGU, header, null, HttpUtils.getUrl(URLConstant.WENGU+URLConstant.WG_SEARCH, params), reqTag);
	}

	private HashMap<String, String> requestHeader() {
		HashMap<String, String> header = new HashMap<String, String>();
		header.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		header.put("Accept-Encoding", "gzip, deflate, sdch");
		header.put("Cache-Control", "max-age=0");
		header.put("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
		header.put("Connection", "keep-alive");
		header.put("Host", "m.iwencai.com");
		header.put("If-Modified-Since", "Wed, 13 Jan 2016 10:41:01 GMT");
		header.put("If-None-Match", "\"1c6001-8325-52934ce602940\"");
		header.put("Upgrade-Insecure-Requests", "1");
//		header.put("Cookie", "Hm_lvt_57a0e026e5e0e1e90bcea3f298e48e74=1461941668; Hm_lpvt_57a0e026e5e0e1e90bcea3f298e48e74=1461941668; user=MDpib2J5empiOjpOb25lOjUwMDoxMTg0NDQzNzA6NywxMTExMTExMTExMSw0MDs0NCwxMSw0MDs2LDEsNDA7NSwxLDQwOjI3Ojo6MTA4NDQ0MzcwOjE0NjE5NDE2NjQ6OjoxMzEwODY3MjgwOjIyNzEzNjow; userid=108444370; u_name=bobyzjb; escapename=bobyzjb; ticket=63b620bd5a4642ab20aa25a09950212a; PHPSESSID=9palul03uadd43n92p3pji3tk6; escapename_wc=bobyzjb; ticket_wc=63b620bd5a4642ab20aa25a09950212a; u_name_wc=bobyzjb; user_wc=MDpib2J5empiOjpOb25lOjUwMDoxMTg0NDQzNzA6NywxMTExMTExMTExMSw0MDs0NCwxMSw0MDs2LDEsNDA7NSwxLDQwOjI3Ojo6MTA4NDQ0MzcwOjE0NjE5NDE2NjQ6OjoxMzEwODY3MjgwOjIyNzEzNjow; userid_wc=108444370; cid=9palul03uadd43n92p3pji3tk61462086541; ComputerID=9palul03uadd43n92p3pji3tk6160501+150901");
		header.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4");
		return header;
	}

	
//	qs:gx_wapclient
//	w:机构调研；股东人数减少；总股本小于5亿股；股东人数小于15000；股价小于50，dde大单净入；总市值小于50亿；总市值从小到大；上市天数大于100
//	source:phone
//	queryarea:all
//	querytype:
//	tid:stockpick
//	cid:kdp608tfek1ja6lq5sf3gmb6501462078279
//	qid:jjao6ntOsr4umiKBJfz5vabGdyqpQHNS
//	perpage:20
//	userid:108444370
//	usertype:m
	private HashMap<String, String> requestParams(String key) {
		HashMap<String, String> params = new HashMap<String, String>();
		try {
			String keyEncode = URLEncoder.encode(key, "utf-8");
			params.put("qs", "gx_wapclient");
			params.put("w", keyEncode);
			params.put("source", "phone");
			params.put("queryarea", "all");
			params.put("querytype", "");
			params.put("tid", "stockpick");
			params.put("userid", "108444370");
			params.put("cid", "kdp608tfek1ja6lq5sf3gmb6501462078279");
			params.put("qid", "jjao6ntOsr4umiKBJfz5vabGdyqpQHNS");
			params.put("perpage", "20");
			params.put("usertype", "m");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return params;
	}

	@Override
	public void onErrorResponse(VolleyError error, EventBusConstants eventCode) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onErrorResponse(VolleyError error, String eventCode) {
		// TODO Auto-generated method stub
		if (eventCode.equals(URLConstant.WG_SEARCH)) {

		}
	}

	@Override
	public void onResponse(String response, EventBusConstants eventCode) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResponse(String response, String eventCode) {
		// TODO 处理经过pase后返回的内容
		if (eventCode.equals(URLConstant.WG_SEARCH)) {
			
		}
	}

	@Override
	public String parseResponse(NetworkResponse response){
		//问股中需要用到的
		ByteArrayInputStream bIn = new ByteArrayInputStream(response.data);
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		try {
			GZIPInputStream gzipInputStream = new GZIPInputStream(bIn);
			byte[] buffer = new byte[256];
			int n ;
			while((n = gzipInputStream.read(buffer))>0){
				bOut.write(buffer, 0, n);
			}
			String s = bOut.toString();
			return s;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	

	
}
